package one.deploy.controller;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import one.deploy.config.IServiceConfigReader;
import one.deploy.dto.ApplicationListResponse;
import one.deploy.dto.ConfigInfoResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author cjr
 */
@Controller
@RequestMapping("/application")
public class ApplicationController {

    @Autowired
    private IServiceConfigReader serviceConfigReader;

    @GetMapping
    public String deploy(HttpServletRequest request, Map<String, Object> model, String key) {
        serviceConfigReader.reload();
        init(model);
        if (StringUtils.equals(key, "asdkf23asdi23@0=")) {
            populateApps(model);
        }
        return "eureka/deploy";
    }

    private void populateApps(Map<String, Object> model) {
        List<IServiceConfigReader.App> appCollections = serviceConfigReader.getApps();
        List<ApplicationListResponse> apps = Lists.newArrayList();
        for (IServiceConfigReader.App app : appCollections) {
            List<ApplicationListResponse.Action> actions = Lists.newArrayList();
            if (Objects.nonNull(app.getActions())) {
                for (Map.Entry<String, IServiceConfigReader.Action> entry : app.getActions().entrySet()) {
                    actions.add(ApplicationListResponse.Action.builder()
                            .name(entry.getKey())
                            .url(entry.getValue().getContent())
                            .build());
                }
            }

            ApplicationListResponse applicationListResponse = ApplicationListResponse.builder()
                    .name(app.getName())
                    .lastVersion(app.getVersion())
                    .actions(actions)
                    .instances(serviceConfigReader.getInstances(app))
                    .build();
            boolean versionLast = true;
            if (CollUtil.isNotEmpty(applicationListResponse.getInstances())) {
                for (ApplicationListResponse.Instance instance : applicationListResponse.getInstances()) {
                    if (!StringUtils.equals(applicationListResponse.getLastVersion(), instance.getVersion())) {
                        versionLast = false;
                        break;
                    }
                }
            }
            applicationListResponse.setVersionLast(versionLast);

            apps.add(applicationListResponse);
        }
        model.put("apps", apps);
    }

    private void init(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("basePath", "/");
        model.put("dashboardPath", "");
        model.put("replicas", null);
        IServiceConfigReader.Config config = serviceConfigReader.getConfig();
        model.put("configInfo", ConfigInfoResponse.builder()
                .path(config.getWorkspacePath())
                .build()
        );
    }
}
